package com.wedo.mall.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.e.library.fragment.EFragment;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.EUtils;
import com.e.library.utils.EViewUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wedo.mall.R;
import com.wedo.mall.WApp;
import com.wedo.mall.activity.MainActivity;
import com.wedo.mall.activity.main.SearchActivity;
import com.wedo.mall.adapter.AgentAdapter;
import com.wedo.mall.adapter.AgentCategoryAdapterX;
import com.wedo.mall.entity.Agent;
import com.wedo.mall.entity.AgentCategory;
import com.wedo.mall.ptr.Pull2Refresh;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lxy on 2017/6/8.
 */

public class AgentFragment extends EFragment<MainActivity> {

    @BindView(R.id.rv_agents) XRecyclerView rv_agents;
    @BindString(R.string.category_more) String category_more;

    public static AgentFragment with() {
        return new AgentFragment();
    }

    @Override
    protected int layout() {
        return R.layout.fragment_agent;
    }

    @Override
    protected void onFragmentCreated(View contentView, Bundle bundle) {

        View header = LayoutInflater.from(getContext()).inflate(R.layout.header_agent_category, null);
        final RecyclerView rv_category = (RecyclerView) header.findViewById(R.id.rv_category);
        final View tv_empty = header.findViewById(R.id.tv_empty);
        rv_category.setHasFixedSize(false);
        rv_category.setNestedScrollingEnabled(false);
        rv_category.setLayoutManager(new GridLayoutManager(getContext(), 4));
        categories(rv_category);

        rv_agents.setHasFixedSize(true);
        EViewUtils.vertical(rv_agents);

        /**商店精品下拉刷新相关*/
        Pull2Refresh<Agent> ptr = new Pull2Refresh<>();
        AgentAdapter adapter = AgentAdapter.search(getContext(),null);
        adapter.listener(activity());
        ptr.widget(rv_agents).adapter(adapter).url(EApi.AGENT_LIST).params(WApp.lngLat()).clazz(Agent.class).emptyWidget(tv_empty).done();

        /**将商品分类添加到XRecyclerView头部*/
        rv_agents.addHeaderView(header);

    }

    /**商品分类*/
    private void categories(final RecyclerView rv_category) {

        EClient.with().post(EApi.AGENT_CATEGORY, new EObserver() {
            @Override
            public void successful(EResponse response) {
                List<AgentCategory> list = response.list(AgentCategory.class);
                List<AgentCategory> categories = sublist(list);
                AgentCategoryAdapterX adapter = new AgentCategoryAdapterX(getContext(), categories);
                rv_category.setAdapter(adapter);
                adapter.listener(activity());
            }

            @Override
            public void failure(String msg) {

            }
        });
    }

    private List<AgentCategory> sublist(List<AgentCategory> list){
        if (EUtils.isEmpty(list)){
            return list;
        }

        AgentCategory more = new AgentCategory();
        more.more = R.drawable.ic_home_more;
        more.category_name = category_more;

        if (list.size() > 7) {
            /**截取前7个分类，第八个分类为"更多分类*/
            List<AgentCategory> categories = new ArrayList<>(8);
            for (int i = 0; i < 7; i++) {
                categories.add(list.get(i));
            }
            categories.add(7, more);
            return categories;
        }
        list.add(more);
        return list;
    }

    @OnClick(R.id.tv_search)
    protected void onSearchClick(){
        SearchActivity.toSearchAgent(activity());
    }

}
